package com.cr.crandroid.adapter;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.viewholder.BaseViewHolder;
import com.cr.crandroid.R;

import com.cr.crandroid.ui.person.approve.ApproveDetailFragment;
import com.cr.crandroid.utils.CommonConstant;
import com.cr.crandroid.vo.CarVo;
import com.google.android.material.dialog.MaterialAlertDialogBuilder;
import com.google.gson.Gson;

import java.util.List;

public class ApproveAdapter extends BaseQuickAdapter<CarVo, BaseViewHolder> {

    private MaterialAlertDialogBuilder materialAlertDialog;
    private LayoutInflater inflater;
    private ViewGroup container;
    private Context context;
    private CarVo nowCar = new CarVo();

    public ApproveAdapter(@Nullable List<CarVo> data) {
        super(R.layout.item_approve, data);
    }

    public ApproveAdapter(@Nullable List<CarVo> data, MaterialAlertDialogBuilder alertDialog, LayoutInflater inflater, ViewGroup container, Context context) {
        super(R.layout.item_approve, data);
        materialAlertDialog = alertDialog;
        this.inflater = inflater;
        this.container = container;
        this.context = context;
    }

    @Override
    protected void convert(@NonNull BaseViewHolder baseViewHolder, CarVo carVo) {
        baseViewHolder.setText(R.id.textView, carVo.getRealName());
        baseViewHolder.setText(R.id.textView2, carVo.getBrand() + "·");
        baseViewHolder.setText(R.id.textView3, carVo.getColor() + "·");
        baseViewHolder.setText(R.id.textView4, carVo.getSeatingCapacity());
        Button button = baseViewHolder.findView(R.id.button);
        ImageView imageView = baseViewHolder.findView(R.id.imageView2);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                showMaterialAlertDialog(carVo);
            }
        });
        switch (carVo.getStatus()) {
            case CommonConstant.APPROVE_STATUS_1:
                imageView.setImageResource(R.drawable.under_approve);
                break;
            case CommonConstant.APPROVE_STATUS_2:
                imageView.setImageResource(R.drawable.pass);
                break;
            case CommonConstant.APPROVE_STATUS_3:
                imageView.setImageResource(R.drawable.not_pass);
                break;
        }
        if (carVo.getRentalStart() != null && carVo.getRentalEnd() != null) {
            baseViewHolder.setText(R.id.textView6, carVo.getRentalStart() + "至" + carVo.getRentalEnd());
        }
        if (carVo.getImage() != null)
            Glide.with(getContext()).load(carVo.getImage().split(",")[0])
                    .into((ImageView) baseViewHolder.getView(R.id.imageView));
    }

    private void showMaterialAlertDialog(CarVo car) {
        nowCar = car;
        Bundle bundle = new Bundle();
        bundle.putString("car", new Gson().toJson(car, CarVo.class));
        materialAlertDialog.setView(new ApproveDetailFragment().onCreateView(inflater, container, bundle, context));
        materialAlertDialog.show();
    }

    public CarVo getNowCar() {
        return nowCar;
    }
}
